using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartLink.DTOS;
using SmartLink.DTOS.Permission;
using SmartLink.Application.IServices;
using SmartLink.Domain;
using SmartLink.Domain.Entity;
using SmartLink.Domain.Services;
using SmartLink.EnumLibrary;
using SmartLink.Infrastructure;
using SmartLink.Infrastructure.Cache;
using SmartLink.IRepository;
using SmartLink.Domain.IServices;
using SmartLink.Domain.ExpandEntity.Base;

namespace SmartLink.Application.Services
{
    [Obsolete]
    public class PermissionManagerAppServices
    {
        private IPermissionServices _permissionsManagerServices;
        private IRoleServices _roleServices;

        public PermissionManagerAppServices(IPermissionServices permissionsManagerServices,
            IRoleServices roleServices)
        {
            _permissionsManagerServices = permissionsManagerServices;
            _roleServices = roleServices;
            //初始化用户角色关系 会每次调用
            //InitUserRoleRelations();
        }

        #region  权限相关操作

        public Results AllotMenuIdToRole(AllotMenuToRoleDTO allotMenuToRoleDTO, int curUserId)
        {
            CurrentUserInfo curUserInfo = UserInfoCaches.GetUserInfoCache(curUserId);
            PER_RoleGroup changeRole = _roleServices.GetRole(allotMenuToRoleDTO.RoleId);
            //不是超管，无权给 超管和公司管理员 分配权限
            if (changeRole.RoleName == ConstData.OrgManagerRoleName && (curUserInfo.RoleName != ConstData.SuperManagerRoleName))
                return InfrastructureAppService.Response("无权给该角色分配权限", StateCodeEnum.失败.GetHashCode());

            bool isOrgManager = changeRole.RoleName == ConstData.OrgManagerRoleName;
            bool sucFlag = _permissionsManagerServices.AllotMenuIdToRole(isOrgManager, allotMenuToRoleDTO.RoleId, allotMenuToRoleDTO.MenuIds);
            if (sucFlag)
            {
                //角色有变化，把相关用户缓存清空
                UserInfoCaches.RemoveUserCacheByRole(allotMenuToRoleDTO.RoleId);
                return InfrastructureAppService.Response("菜单权限分配成功", StateCodeEnum.成功.GetHashCode());
            }
            else
                return InfrastructureAppService.Response("菜单权限分配失败", StateCodeEnum.失败.GetHashCode());
        }

        public Results AllotActionsToRole(AllotActionToRoleDTO allotActionToRoleDTO)
        {
            bool sucFlag = _permissionsManagerServices.AllotActionsToRole(allotActionToRoleDTO.RoleId, allotActionToRoleDTO.ActionIds);
            if (sucFlag)
            {
                UserInfoCaches.RemoveUserCacheByRole(allotActionToRoleDTO.RoleId);
                return InfrastructureAppService.Response("操作权限分配成功", StateCodeEnum.成功.GetHashCode());
            }
            else
                return InfrastructureAppService.Response("操作权限分配失败", StateCodeEnum.失败.GetHashCode());
        }

        public Results AllotDataRightsToRole(AllotDataRightsToRole allotDataRightsToRole)
        {
            bool sucFlag = _permissionsManagerServices.AllotDataRightsToRole(allotDataRightsToRole.RoleId, allotDataRightsToRole.ODIDS);
            if (sucFlag)
            {
                UserInfoCaches.RemoveUserCacheByRole(allotDataRightsToRole.RoleId);
                return InfrastructureAppService.Response("数据权限分配成功", StateCodeEnum.成功.GetHashCode());
            }
            else
                return InfrastructureAppService.Response("数据权限分配失败", StateCodeEnum.失败.GetHashCode());
        }

        #endregion
    }
}
